<template>
  <page-main class="page-patient-list">
    <div class="p-t-15"></div>
    <van-empty description="暂时没有就诊人哦" v-if="list.length == 0"/>
    <van-cell-group v-else inset v-for="(item,index) in list" :key="index">
      <van-cell :border="false" :label="$util.desNumber(item.idNo)" @click="$util.gotoPage('/patient/addEdit', {patientId: item.patientId, type: 'edit'})" >
        <div class="title" slot="title">
          <span>{{item.custName}}</span>
          <van-tag round class="tag-blue-light m-l-5">{{$util.setName(item.relationType, init_data.relationType_list)}}</van-tag>
        </div>
        <div class="clearfix">
          <van-switch v-model="item.isDefault == '1' ? true : false" @click.stop="changeDefault($event,item,index)"/>
          <span class="m-r-5">{{item.isDefault == '1' ? '默认' : '设置默认'}}</span>
        </div>
      </van-cell>
    </van-cell-group>
    <div class="p-a-15">
      <van-button type="info" block @click="$util.gotoPage('/patient/addEdit', {type: 'add'})">添加就诊人</van-button>
    </div>
  </page-main>
</template>
<script>
export default {
  data(){
    return {
      list:[],
      init_data:{
        relationType_list: []
      },
    }
  },
  created(){
    this.getInitData();
    this.getList();
  },
  methods:{
    async getInitData(){
      this.init_data.relationType_list = await this.$util.getDictList('relation_type');
    },
    getList(){
      this.$http('get', '/admin/custApi/getPatientsList', {}, (data,msg) => {
        this.list = data;
        this.$store.dispatch('getCurrentPatient');
      })
    },
    changeDefault(e,item,index){
      if(item.isDefault == '0'){
        // let obj = Object.assign(item, {isDefault: '1'});
        let obj = {
          address: item.address,
          isDefault: 1,
          patientId: item.patientId,
          phone: item.phone,
          remark: item.remark,
          relationType: item.relationType,
        };
        this.$http('put', '/admin/custApi', obj , (data,msg) => {
        this.$toast.success('设置成功！');
        this.getList();        
      },msg => {
        this.list[index].isDefault = '1';
      })
      }
    }
  }
}
</script>

<style>

</style>